/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.plian.system.controller.sys.formhistoryreord;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.entity.sys.BaseFormColumnName;
import com.plian.system.entity.sys.SearchColumnName;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.sys.system.IBaseFormColumnNameService;
import com.plian.system.service.sys.system.ISearchColumnNameService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 控制器
 *
 * @author
 * @since 2019-09-27
 */
@RestController
@AllArgsConstructor
@RequestMapping("api/baseformcolumnname")
public class BaseFormColumnNameController extends BaseController {

	private IBaseFormColumnNameService baseFormColumnNameService;

	private ISearchColumnNameService searchColumnNameService;

	/**
	 * 根据表名创建
	 */
	@PostMapping("/create")
	public R create(@RequestBody BaseFormColumnName baseFormColumnName) {
		return baseFormColumnNameService.createByTableName(baseFormColumnName);
	}

	@PutMapping("/updateSearchColumn/{id}")
	public R updateSearchColumn(@PathVariable Long id){
		return baseFormColumnNameService.updateSearchColumn(id);
	}

	@GetMapping("/detail/{id}")
	public R<BaseFormColumnName> recordList(@PathVariable Long id){
		BaseFormColumnName baseFormColumnName = baseFormColumnNameService.detail(id);
		R<BaseFormColumnName> obj = new R<>(CommonCode.SUCCESS);
		obj.setPageData(baseFormColumnName);
		return obj;
	}

	@GetMapping("/getSearchColumn/{formType}")
	public ListResult<SearchColumnName> getSearchColumn(@PathVariable String formType){
		ListResult<SearchColumnName> listResult = new ListResult<>(CommonCode.SUCCESS);
		listResult.setList(searchColumnNameService.list(new LambdaQueryWrapper<SearchColumnName>()
				.eq(SearchColumnName::getFormType, formType)
				.eq(SearchColumnName::getIsSearch, 2)));
		return listResult;
	}

	/**
	 * 分页
	 */
	@PostMapping("/list")
	public ListResult<BaseFormColumnName> list( @RequestBody Query query) {
		List<SearchEntity> entityList = query.getSearch();
		QueryWrapper<BaseFormColumnName> queryWrapper = SearchParamEntiy.getQueryWrapper(
				Condition.getQueryWrapper(new BaseFormColumnName()), entityList
		);
		IPage<BaseFormColumnName> pages = baseFormColumnNameService.page(Condition.getPage(query), queryWrapper);
		ListResult<BaseFormColumnName> listResult = new ListResult<>(CommonCode.SUCCESS);
		listResult.setList(pages.getRecords());
		listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
		return listResult;
	}

	@PostMapping("/edit")
	public R edit(@RequestBody BaseFormColumnName baseFormColumnName){
		return baseFormColumnNameService.edit(baseFormColumnName);
	}

	/**
	 * 删除
	 */
	@DeleteMapping("/remove")
	public ObjResult remove(@RequestBody PageData pd) {
		List<String> ids = (List<String>)pd.get("ids");
		return baseFormColumnNameService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
				? new ObjResult(CommonCode.SUCCESS) : new ObjResult(CommonCode.FAIL);
	}


}
